﻿namespace Chain.Tasks
{
    using System;
    using System.Xml;

    public class Task
    {
        private readonly XmlNode _configNode;
        private bool _enabled = true;
        private bool _isRunning;
        private DateTime _lastEnd;
        private DateTime _lastStarted;
        private DateTime _lastSuccess;
        private readonly string _name;
        private readonly bool _stopOnError;
        private ITask _task;
        private readonly Type _taskType;

        public Task(Type taskType, XmlNode configNode)
        {
            this._configNode = configNode;
            this._taskType = taskType;
            if ((configNode.Attributes["enabled"] != null) && !bool.TryParse(configNode.Attributes["enabled"].Value, out this._enabled))
            {
                this._enabled = true;
            }
            if ((configNode.Attributes["stopOnError"] != null) && !bool.TryParse(configNode.Attributes["stopOnError"].Value, out this._stopOnError))
            {
                this._stopOnError = true;
            }
            if (configNode.Attributes["name"] != null)
            {
                this._name = configNode.Attributes["name"].Value;
            }
        }

        private ITask createTask()
        {
            if (this._enabled && (this._task == null))
            {
                if (this._taskType != null)
                {
                    this._task = Activator.CreateInstance(this._taskType) as ITask;
                }
                this._enabled = this._task != null;
            }
            return this._task;
        }

        public void Execute()
        {
            this._isRunning = true;
            try
            {
                ITask task = this.createTask();
                if (task != null)
                {
                    this._lastStarted = DateTime.Now;
                    task.Execute(this._configNode);
                    this._lastEnd = this._lastSuccess = DateTime.Now;
                }
            }
            catch (Exception)
            {
                this._enabled = !this._stopOnError;
                this._lastEnd = DateTime.Now;
            }
            this._isRunning = false;
        }

        public string Name
        {
            get
            {
                return this._name;
            }
        }
    }
}

